﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace Jacaranda.Model
{
    public class AbstractProperty : ComparableNode
    {
        public AbstractProperty(string name)
        {
            Name = name;
        }

        public AbstractProperty(Type entityType, string name)
        {
            EntityType = entityType;
            Name = name;
        }


        public Type EntityType { get; private set; }

        public string Name { get; private set; }

        internal override string GenerateSql(Type propertyEntityType)
        {
            string fieldName = Metadata.Type(EntityType ?? propertyEntityType).Fields.FirstOrDefault(i => i.PropertyName == Name).FieldName;

            if (EntityType != null)
            {
                fieldName = (Metadata.Type(EntityType)).TableName + "." + fieldName;
            }

            return fieldName;
        }
    }
}
